*Analysis Do-File for creating Figure 1
*Runs diff-in-difference (DD) regressions for arrests and produces coefficient plots

***File set-up
macro drop _all
mat drop _all
eststo clear
estimates clear
global path "[ENTER PATH HERE]"
global figs "$path\REStat Replication V2\Figures"
global log "$path\REStat Replication V2\Log Files"
set scheme lean2

clear
set more off

cap log close
log using "$log\figure_1.txt", replace text
use "$path\REStat Replication V2\Data\master_data_analysis_subset"

*Set folder for main figures
cd "$figs"


***DD regression: Arrests on timing of the highest-profile police killing per jursidiction
*Creates estimates for Figure 1 and Table E3-5

***Estimates for Violent Crime Arrests
local outcome index_violent murder_tot agg_assault_tot
local descrip `""Panel A: Violent Crime Arrests" "Panel B: Murder Arrests" "Panel C: Aggravated Assault Arrests""'

*Run loop for each outcome
	*Could also use gettoken (search about "parallel list structure")
forvalues i = 1/3{
	local arrest : word `i' of `outcome'
	local panel : word `i' of `descrip'

	
foreach var of varlist `arrest' {

	di "Analysis by qtr for extended event window"
	di "Highest profile OF DiD in ori"
	eststo cln_`var'_hi: reghdfe ln_`var' ln_pop_s ///
		treat_post_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	mat coef_`var' = r(table)'
	mat coef_`var' = (coef_`var'[rownumb(coef_`var',"treat_post_high"), ///
		colnumb(coef_`var',"b")..colnumb(coef_`var',"se")])		
		
	di "Pre-trend regressions DiD"
	eststo cln_`var'_hip: reghdfe ln_`var' ln_pop_s ///
		treat_post_high pretrend_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	di "HiOF Trip Diff- Fully Saturated"
	*Pop ctrl and treat_post with binned endpoints are thrown out because of month#ori FEs
		*Need to include pop ctrl still to ensure reg is still run on obs where pop is non-missing
		*Also include double indicator (even though terms drop) to keep signs same
			*Changes reference point otherwise
	eststo cln_`var'_3d: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high neg_window_high##blk pos_window_high##blk ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)		

	di "Pre-trend Triple Diff- Fully Saturated"
	eststo cln_`var'_3dp: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high blk_pretrend_high pretrend_high neg_window_high##blk pos_window_high##blk ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)
		
	}
}

***Estimates for Property Crime Arrests
local outcome index_property burglary_tot theft_tot
local descrip `""Panel A: Property Crime Arrests" "Panel B: Burglary Arrests" "Panel C: Theft Arrests""'

*Run loop for each outcome
	*Could also use gettoken (search about "parallel list structure")
forvalues i = 1/3{
	local arrest : word `i' of `outcome'
	local panel : word `i' of `descrip'

foreach var of varlist `arrest' {

	di "Analysis by qtr for extended event window"
	di "Highest profile OF DiD in ori"
	eststo cln_`var'_hi: reghdfe ln_`var' ln_pop_s ///
		treat_post_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	mat coef_`var' = r(table)'
	mat coef_`var' = (coef_`var'[rownumb(coef_`var',"treat_post_high"), ///
		colnumb(coef_`var',"b")..colnumb(coef_`var',"se")])		
	
	di "Pre-trend regressions DiD"
	eststo cln_`var'_hip: reghdfe ln_`var' ln_pop_s ///
		treat_post_high pretrend_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	di "HiOF Trip Diff- Fully Saturated"
	*Pop ctrl and treat_post with binned endpoints are thrown out because of month#ori FEs
		*Need to include pop ctrl still to subset obs on same 
		*Also include double indicator (even though terms drop) to keep signs same
			*Changes reference point otherwise
	eststo cln_`var'_3d: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high neg_window_high##blk pos_window_high##blk ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)

	di "Pre-trend Triple Diff- Fully Saturated"
	eststo cln_`var'_3dp: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high blk_pretrend_high pretrend_high neg_window_high##blk pos_window_high##blk ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)
	}

}

***Estimates for Low-level Arrests
local outcome lowMB disorder_cond_tot poss_cannabis_tot
local descrip `""Panel A: Low-level Arrests" "Panel B: Disorderly Conduct Arrests" "Panel C: Marijuana Possession Arrests" "'

*Run loop for each outcome
	*Could also use gettoken (search about "parallel list structure")
forvalues i = 1/3{
	local arrest : word `i' of `outcome'
	local panel : word `i' of `descrip'

foreach var of varlist `arrest' {

	di "Analysis by qtr for extended event window"
	di "Highest profile OF DiD in ori"
	eststo cln_`var'_hi: reghdfe ln_`var' ln_pop_s ///
		treat_post_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	mat coef_`var' = r(table)'
	mat coef_`var' = (coef_`var'[rownumb(coef_`var',"treat_post_high"), ///
		colnumb(coef_`var',"b")..colnumb(coef_`var',"se")])			
	
	di "Pre-trend regressions DiD"
	eststo cln_`var'_hip: reghdfe ln_`var' ln_pop_s ///
		treat_post_high pretrend_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
		
	di "HiOF Trip Diff- Fully Saturated"
	*Pop ctrl and treat_post with binned endpoints are thrown out because of month#ori FEs
		*Need to include pop ctrl still to subset obs on same 
		*Also include double indicator (even though terms drop) to keep signs same
			*Changes reference point otherwise
	eststo cln_`var'_3d: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high neg_window_high##blk pos_window_high##blk ///
		if !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)

	di "Pre-trend Triple Diff- Fully Saturated"
	eststo cln_`var'_3dp: reghdfe ln_`var' ln_pop_s ///
		blk_treat_post_high treat_post_high blk_pretrend_high pretrend_high neg_window_high##blk pos_window_high##blk ///
		if  !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(blk#group_ori blk#month_year month_year#group_ori)
	}

}

*Run robbery, marijuana sale, and motor vehicle theft (not in above tables)
	*Separate loops because of checking if crime data is missing for part I offenses
local arrest robbery_tot mtr_veh_theft_tot 
foreach var of varlist `arrest' {

	***Analysis by qtr for extended event window w/ Part I offense ctrl
	*Highest profile OF DiD in ori
	reghdfe ln_`var' ln_pop_s ///
		treat_post_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year)
	
	mat coef_`var' = r(table)'
	mat coef_`var' = (coef_`var'[rownumb(coef_`var',"treat_post_high"), ///
		colnumb(coef_`var',"b")..colnumb(coef_`var',"se")])	
	
	*Pre-trend regressions DiD
	reghdfe ln_`var' ln_pop_s ///
		treat_post_high pretrend_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_`var', ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
}

local arrest sale_cannabis_tot
foreach var of varlist `arrest' {

	***Analysis by qtr for extended event window w/ Part I offense ctrl
	*Highest profile OF DiD in ori
	reghdfe ln_`var' ln_pop_s ///
		treat_post_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
	
	mat coef_`var' = r(table)'
	mat coef_`var' = (coef_`var'[rownumb(coef_`var',"treat_post_high"), ///
		colnumb(coef_`var',"b")..colnumb(coef_`var',"se")])	
	
	*Pre-trend regressions DiD
	reghdfe ln_`var' ln_pop_s ///
		treat_post_high pretrend_high neg_window_high pos_window_high ///
		if !inlist(., ln_actual_index_violent, ln_actual_index_property) ///
		, cl(group_ori) absorb(group_ori month_year ///
		)
}

****Create DD arrest coefplot
mat DD_long = coef_murder_tot \ coef_agg_assault_tot \ coef_robbery_tot \ ///
	coef_mtr_veh_theft_tot \ coef_burglary_tot \ coef_theft_tot	\ ///
	coef_sale_cannabis_tot \ coef_disorder_cond_tot \ coef_poss_cannabis_tot
	
mat colnames DD_long = b se

mat rownames DD_long = "Murder" "Aggravated Assault" "Robbery" "Motor Vehicle Theft*" "Burglary" "Theft" ///
	"Marijuana Sale" "Disorderly Conduct" "Marijuana Possession"

coefplot mat(DD_long[,1]), horizontal xline(0, lcolor(red)) se(DD_long[,2])  /// 
	ylabel(, angle(0) glcolor(gs12) nogrid) ///
	xlabel(, glcolor(gs12) grid) grid(none) ///
	msize(large) msymbol(O) mfcolor(black%100) mcolor(white%0) ///
	xtitle(Change in Log Points) ytitle(Arrest Category) ///
	headings(Murder = "{bf:Violent Crime}" "Motor Vehicle Theft*" = "{bf:Property Crime}" ///
	"Marijuana Sale" = "{bf:Low-level Crime}") ///
	note("*There is a significant difference between the average pre-treatment trends of" ///
		"control and treated departments for motor vehicle theft arrests, but no others.")
graph export "$figs/figure_1.pdf", replace


cap log close

